home *** CD-ROM | disk | FTP | other *** search
/ Scene 96 / Scene 96 International Edition (Zyklop Software) (Disc 2) (1997).iso / misc / coding / midas060 / samples / api / effects.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-17  |  4.3 KB  |  173 lines

  1. /*      effects.c
  2.  *
  3.  * A small sound effects example with the DLL API
  4.  *
  5.  * Copyright 1996,1997 Housemarque Inc.
  6.  *
  7.  * This file is part of the MIDAS Sound System, and may only be
  8.  * used, modified and distributed under the terms of the MIDAS
  9.  * Sound System license, LICENSE.TXT. By continuing to use,
  10.  * modify or distribute this file you indicate that you have
  11.  * read the license and understand and accept it fully.
  12. */
  13.  
  14. #if defined(__NT__) || defined(__WINDOWS__) || defined(_MSC_VER)
  15. #define WIN32_LEAN_AND_MEAN
  16. #include <windows.h>
  17. #endif
  18.  
  19. #include <stdio.h>
  20. #include <conio.h>
  21. #include <stdlib.h>
  22.  
  23. #ifdef __DOS__
  24. #include <dos.h>
  25. #endif
  26.  
  27. #include "midasdll.h"
  28.  
  29. /* Use delay() for delays in DOS, Sleep() in Win32: */
  30. #ifdef __DOS__
  31.     #define DELAY(x) delay(x)
  32. #else
  33.     #define DELAY(x) Sleep(x)
  34. #endif
  35.  
  36.  
  37. /****************************************************************************\
  38. *
  39. * Function:     void MIDASerror(void)
  40. *
  41. * Description:  Handles a MIDAS error - displays an error message and exits
  42. *
  43. \****************************************************************************/
  44.  
  45. void MIDASerror(void)
  46. {
  47.     int         error;
  48.  
  49.     error = MIDASgetLastError();
  50.     printf("\nMIDAS error: %s\n", MIDASgetErrorMessage(error));
  51.     if ( !MIDASclose() )
  52.     {
  53.         printf("\nBIG PANIC! MIDASclose Failed: %s\n", MIDASgetErrorMessage(
  54.             MIDASgetLastError()));
  55.     }
  56.     exit(EXIT_FAILURE);
  57. }
  58.  
  59.  
  60. #define SWEEPSTEPS 16
  61.  
  62.  
  63. static MIDASsample sample1, sample2;
  64. static MIDASsamplePlayHandle playHandles[SWEEPSTEPS];
  65. static MIDASsamplePlayHandle laugh;
  66.  
  67.  
  68. int main(void)
  69. {
  70.     int         pan;
  71.     unsigned    i;
  72.  
  73.     MIDASstartup();
  74.  
  75.     setbuf(stdout, NULL);
  76.  
  77.     /* Flag that we don't have a module, effects or streams playing: */
  78.     sample1 = sample2 = 0;
  79.     for ( i = 0; i < 16; i++ )
  80.         playHandles[i] = 0;
  81.     laugh = 0;
  82.  
  83.     /* Decrease the size of buffer used: */
  84.     MIDASsetOption(MIDAS_OPTION_MIXBUFLEN, 150);
  85.     MIDASsetOption(MIDAS_OPTION_MIXBUFBLOCKS, 4);
  86.  
  87.     /* Initialize MIDAS and start background playback (at 100 polls
  88.        per second): */
  89.     if ( !MIDASinit() )
  90.         MIDASerror();
  91.     if ( !MIDASstartBackgroundPlay(100) )
  92.         MIDASerror();
  93.  
  94.     /* Open five channels for our effects:  */
  95.     if ( !MIDASopenChannels(5) )
  96.         MIDASerror();
  97.  
  98.     /* Use four first channels for automatic sound effects:
  99.        (the last one will be used for a background laugh) */
  100.     if ( !MIDASsetAutoEffectChannels(0, 4) )
  101.         MIDASerror();
  102.  
  103.     /* Load our samples: */
  104.     if ( (sample1 = MIDASloadRawSample("..\\data\\explosi1.pcm",
  105.         MIDAS_SAMPLE_8BIT_MONO, MIDAS_LOOP_NO)) == 0 )
  106.         MIDASerror();
  107.     if ( (sample2 = MIDASloadRawSample("..\\data\\laugh1.pcm",
  108.         MIDAS_SAMPLE_8BIT_MONO, MIDAS_LOOP_YES)) == 0 )
  109.         MIDASerror();
  110.  
  111.     /* Start the laugh on channel 4 at volume 0: */
  112.     if ( (laugh = MIDASplaySample(sample2, 4, 0, 16000, 0,
  113.         MIDAS_PAN_SURROUND)) == 0 )
  114.         MIDASerror();
  115.  
  116.     /* Fade laugh in: */
  117.     for ( i = 0; i <= 64; i++ )
  118.     {
  119.         if ( !MIDASsetSampleVolume(laugh, i) )
  120.             MIDASerror();
  121.         DELAY(20);
  122.     }
  123.  
  124.     /* Play machine gun fire, sweep panning from left to right: */
  125.     pan = MIDAS_PAN_LEFT;
  126.     for ( i = 0; i < SWEEPSTEPS; i++ )
  127.     {
  128.         if ( (playHandles[1] = MIDASplaySample(sample1, MIDAS_CHANNEL_AUTO,
  129.             0, 22050, 32, pan)) == 0 )
  130.             MIDASerror();
  131.  
  132.         pan += (MIDAS_PAN_RIGHT - MIDAS_PAN_LEFT) / SWEEPSTEPS;
  133.  
  134.         DELAY(200);
  135.     }
  136.  
  137.     /* Wait for a while for all gun sounds to stop: */
  138.     DELAY(500);
  139.  
  140.     /* Stop the gun sounds: */
  141.     for ( i = 0; i < SWEEPSTEPS; i++ )
  142.     {
  143.         if ( !MIDASstopSample(playHandles[i]) )
  144.             MIDASerror();
  145.     }
  146.  
  147.     /* Fade laugh out: */
  148.     for ( i = 64; i > 0; i-- )
  149.     {
  150.         if ( !MIDASsetSampleVolume(laugh, i) )
  151.             MIDASerror();
  152.         DELAY(30);
  153.     }
  154.  
  155.     /* Stop laugh: */
  156.     if ( !MIDASstopSample(laugh) )
  157.         MIDASerror();
  158.  
  159.     /* Free the samples: */
  160.     if ( !MIDASfreeSample(sample1) )
  161.         MIDASerror();
  162.     if ( !MIDASfreeSample(sample2) )
  163.         MIDASerror();
  164.  
  165.     /* Stop MIDAS: */
  166.     if ( !MIDASstopBackgroundPlay() )
  167.         MIDASerror();
  168.     if ( !MIDASclose() )
  169.         MIDASerror();
  170.  
  171.     return 0;
  172. }
  173.